# get rid of that stupid cache mechanism
rm -f config.cache

AC_INIT(configure.ac)
AC_CANONICAL_BUILD
AC_CANONICAL_HOST
AC_ISC_POSIX
AM_INIT_AUTOMAKE(cmaid, 0.0.1)
AM_CONFIG_HEADER(config.h)

AC_PROG_CC
AC_PROG_CXX
AM_PROG_CC_STDC
AC_HEADER_STDC
AC_C_CONST
AM_ENABLE_SHARED
AM_PROG_LIBTOOL

MODULE_ARCH="$host_os-$host_cpu"
AC_SUBST(MODULE_ARCH)
AC_DEFINE_UNQUOTED(MODULE_ARCH, "$MODULE_ARCH", "Module architecture")

create_shared_lib=""
case "$host_os" in
    *bsd*)
       AC_DEFINE(HAVE_BSD, [], "Have a BSD distribution")
    ;;
    linux*)
       AC_DEFINE(HAVE_LINUX, [], "Have a Linux distribution")
    ;;
    mingw|mingw32)
	create_shared_lib="-no-undefined"
    ;;
esac
AC_SUBST(create_shared_lib)

# Debug
AC_ARG_ENABLE(debug,
	[AC_HELP_STRING([--enable-debug],[enable debug support])],
	[enable_debug=$enableval], [enable_debug="yes"])
AC_MSG_CHECKING([whether to build in debug mode])
AC_MSG_RESULT(${enable_debug})

if test "x${enable_debug}" = "xyes" ; then
  AC_DEFINE(CMAID_ENABLE_DEBUG, 1, [Enable debug support])
else
  AC_DEFINE(CMAID_ENABLE_DEBUG, 0, [Enable debug support])
  enable_debug="no"
fi

AM_CONDITIONAL(CMAID_ENABLE_DEBUG, test "x${enable_debug}" = "xyes")

dnl Do we have gtest?
build_gtests="no"
AC_PATH_PROG(GTEST_CONFIG, [gtest-config])
AM_CONDITIONAL(HAVE_GTEST, test "x$GTEST_CONFIG" != "x")
if test "x$GTEST_CONFIG" != "x"; then
  GTEST_CPPFLAGS=`$GTEST_CONFIG --cppflags`
  AC_SUBST(GTEST_CPPFLAGS)
  GTEST_CXXFLAGS=`$GTEST_CONFIG --cxxflags`
  AC_SUBST(GTEST_CXXFLAGS)
  GTEST_LIBS=`$GTEST_CONFIG --libs --ldflags`
  AC_SUBST(GTEST_LIBS)
  build_gtests="yes"
fi

# Coverage
AC_ARG_ENABLE(gcov,
  AC_HELP_STRING([--enable-gcov],
  [compile with coverage profiling instrumentation]),
  [enable_gcov=$enableval],
  [enable_gcov="no"]
)
AC_MSG_CHECKING([whether to use profiling instrumentation])
AC_MSG_RESULT(${enable_gcov})

if test "x${enable_gcov}" = "xyes" ; then
  AC_CHECK_PROG(have_lcov, [lcov], [yes], [no])

  if test "x${have_lcov}" = "xyes" ; then
    GCOV_CFLAGS="-fprofile-arcs -ftest-coverage"
    GCOV_LIBS="-lgcov"
  else
    AC_MSG_WARN([lcov is not found, disable profiling instrumentation])
    enable_gcov="no"
  fi
fi
AC_SUBST(GCOV_CFLAGS)
AC_SUBST(GCOV_LIBS)

AM_CONDITIONAL(CMAID_ENABLE_GCOV, test "x${enable_gcov}" = "xyes")


AC_OUTPUT([
Makefile
cmaid.pc
src/Makefile
src/lib/Makefile
src/gtests/Makefile
],[
])


#####################################################################
## Info

echo
echo "------------------------------------------------------------------------"
echo "     ________  ______    ________  "
echo "    / ____/  |/  /   |  /  _/ __ \ "
echo "   / /   / /|_/ / /| |  / // / / / "
echo "  / /___/ /  / / ___ |_/ // /_/ /  "
echo "  \____/_/  /_/_/  |_/___/_____/    $VERSION"
echo "                                 "
echo "------------------------------------------------------------------------"
echo
echo
echo "Configuration Options Summary:"
echo
echo "Installation Path.........: $prefix"
echo "Debug Support.............: ${enable_debug}"
echo "Build Unit Tests..........: ${build_gtests}"
echo "Coverage Support..........: ${enable_gcov}"
echo
echo "Now type 'make' ('gmake' on some systems) to compile ${PACKAGE}."
echo
